<template>
  <div class="saved-names" v-if="savedNames.length > 0">
    <h2 class="saved-title">保存的名字</h2>
    
    <div class="saved-grid">
      <div 
        v-for="name in savedNames" 
        :key="name.fullName"
        class="saved-card"
      >
        <!-- 名字主体 -->
        <div class="name-main">
          <h3 class="full-name">{{ name.fullName }}</h3>
          <p class="meaning">{{ name.meaning }}</p>
        </div>
        
        <!-- 详细信息 -->
        <div class="name-details">
          <div class="detail-item">
            <span class="detail-label">五行：</span>
            <span class="detail-value wuxing">{{ name.wuxing }}</span>
          </div>
          <div class="detail-item">
            <span class="detail-label">生肖：</span>
            <span class="detail-value zodiac">{{ name.zodiac }}</span>
          </div>
          <div class="detail-item">
            <span class="detail-label">评分：</span>
            <span class="detail-value score">{{ name.score }}分</span>
          </div>
        </div>
        
        <!-- 分析说明 -->
        <div class="analysis">
          <p>{{ name.analysis }}</p>
        </div>
        
        <!-- 操作按钮 -->
        <div class="actions">
          <button 
            @click="removeName(name)"
            class="action-btn remove-btn"
          >
            删除
          </button>
          <button 
            @click="copyName(name)"
            class="action-btn copy-btn"
          >
            复制
          </button>
        </div>
      </div>
    </div>
    
    <!-- 清空所有保存的名字 -->
    <div class="clear-all">
      <button 
        @click="clearAllNames"
        class="clear-all-btn"
      >
        清空所有保存的名字
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { useNameStore } from '@/stores/nameStore';
import type { GeneratedName } from '@/utils/nameGenerator';

const nameStore = useNameStore();

// 计算属性
const savedNames = computed(() => nameStore.savedNames);

// 删除名字
const removeName = (name: GeneratedName) => {
  nameStore.removeSavedName(name);
};

// 复制名字
const copyName = async (name: GeneratedName) => {
  try {
    await navigator.clipboard.writeText(name.fullName);
    // 这里可以添加一个提示，比如使用toast通知
    alert(`已复制名字：${name.fullName}`);
  } catch (error) {
    console.error('复制失败:', error);
    // 降级方案
    const textArea = document.createElement('textarea');
    textArea.value = name.fullName;
    document.body.appendChild(textArea);
    textArea.select();
    document.execCommand('copy');
    document.body.removeChild(textArea);
    alert(`已复制名字：${name.fullName}`);
  }
};

// 清空所有保存的名字
const clearAllNames = () => {
  if (confirm('确定要清空所有保存的名字吗？')) {
    // 这里需要在store中添加清空所有的方法
    // 暂时使用循环删除的方式
    const namesToRemove = [...savedNames.value];
    namesToRemove.forEach(name => {
      nameStore.removeSavedName(name);
    });
  }
};
</script>

<style scoped>
.saved-names {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.saved-title {
  text-align: center;
  color: #333;
  margin-bottom: 2rem;
  font-size: 1.8rem;
  font-weight: 600;
}

.saved-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
  gap: 2rem;
  margin-bottom: 2rem;
}

.saved-card {
  background: white;
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  border: 2px solid #e5e7eb;
}

.saved-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.name-main {
  text-align: center;
  margin-bottom: 1.5rem;
}

.full-name {
  font-size: 2.5rem;
  font-weight: bold;
  color: #1f2937;
  margin: 0 0 0.5rem 0;
  letter-spacing: 0.1em;
}

.meaning {
  font-size: 1.1rem;
  color: #6b7280;
  margin: 0;
  font-style: italic;
}

.name-details {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  margin-bottom: 1.5rem;
  padding: 1rem;
  background: #f9fafb;
  border-radius: 8px;
}

.detail-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.detail-label {
  font-size: 0.8rem;
  color: #6b7280;
  margin-bottom: 0.25rem;
}

.detail-value {
  font-weight: 600;
  font-size: 1rem;
}

.wuxing {
  color: #059669;
}

.zodiac {
  color: #dc2626;
}

.score {
  color: #7c3aed;
}

.analysis {
  margin-bottom: 1.5rem;
  padding: 1rem;
  background: #f3f4f6;
  border-radius: 8px;
  border-left: 4px solid #10b981;
}

.analysis p {
  margin: 0;
  color: #374151;
  line-height: 1.6;
  font-size: 0.9rem;
}

.actions {
  display: flex;
  gap: 1rem;
}

.action-btn {
  flex: 1;
  padding: 0.75rem;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.remove-btn {
  background: linear-gradient(135deg, #ef4444, #dc2626);
  color: white;
}

.remove-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3);
}

.copy-btn {
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  color: white;
}

.copy-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.clear-all {
  text-align: center;
  margin-top: 2rem;
}

.clear-all-btn {
  background: linear-gradient(135deg, #6b7280, #4b5563);
  color: white;
  border: none;
  padding: 1rem 2rem;
  border-radius: 8px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.clear-all-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(107, 114, 128, 0.3);
}

@media (max-width: 768px) {
  .saved-grid {
    grid-template-columns: 1fr;
  }
  
  .name-details {
    grid-template-columns: 1fr;
    gap: 0.5rem;
  }
  
  .actions {
    flex-direction: column;
  }
}
</style> 